package com.sf.dlb.service;

import com.sf.dlb.config.Global;
import com.sf.dlb.jpa.model.*;
import com.sf.dlb.jpa.repository.*;
import com.sf.dlb.utils.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by jing.tan on 2018/5/21.
 */
@Service
public class UserNewService {
    private static Logger logger = LoggerFactory.getLogger(UserNewService.class);
    @Autowired
    private UserWalletRepository userWalletRepository;
    @Autowired
    private UserNewRepository userNewRepository;
    @Resource
    private Global global;
    /**
     * 更新用户新闻-时长/奖励
     */
    @Transactional
    public UserNewInfo updateUserNew(String userId,Integer currentReadTime,Double currentGetdlbNumber){
        logger.info("updateUserNew userId={},currentReadTime={}",userId,currentReadTime);
        UserNewInfo userNewInfo= userNewRepository.findByUserIdAndDelFlag(userId,"0");
        if(null==userNewInfo){
            logger.info("updateUserNew userNewInfo is null");
            userNewInfo=new UserNewInfo();
            userNewInfo.setCreateDate(new Date());
            userNewInfo.setUserId(userId);
        }
        //阅读时长
        if(null!=currentReadTime){
            Integer newCountTime=userNewInfo.getReadTimeLong();//阅读时间
            userNewInfo.setReadTimeLong(null==newCountTime?currentReadTime:newCountTime+currentReadTime);
            Integer newCount=userNewInfo.getReadCount(); //阅读数量
            userNewInfo.setReadCount(null==newCount?1:newCount+1);
            userNewInfo.setUpdateDate(new Date());
            logger.info("updateUserNew 阅读时长 userNewInfo={}",GsonUtils.getInstance().toJson(userNewInfo));
        }
        //阅读奖励
        if(null!=currentGetdlbNumber){
            Double getDlbCount=userNewInfo.getHistoryGetdlbCount();
            getDlbCount=(null==getDlbCount?0.0:getDlbCount);
            userNewInfo.setHistoryGetdlbCount(Arith.add(getDlbCount,currentGetdlbNumber));
            userNewInfo.setUpdateDate(new Date());
            logger.info("updateUserNew 阅读奖励 userNewInfo={}",GsonUtils.getInstance().toJson(userNewInfo));
        }
        userNewInfo= userNewRepository.save(userNewInfo);
        return userNewInfo;
    }
    /**
     * 初始化用户-新闻阅读信息
     */
    @Transactional
    public UserNewInfo initUserNewInfo(String userId){
        logger.info("initUserNewInfo userId={}",userId);
        UserNewInfo userNewInfo=new UserNewInfo();
        userNewInfo.setUserId(userId);
        userNewInfo.setClassIds(global.new_sub_default_class);
        userNewInfo.setReadCount(0);
        userNewInfo.setReadTimeLong(0);
        userNewInfo.setHistoryGetdlbCount(0.0);
        userNewInfo.setReadGetdlbUnit(global.wallet_read_getdlb_yield);
        userNewInfo.setCreateDate(new Date());
        userNewInfo.setUpdateDate(new Date());
        userNewInfo=userNewRepository.save(userNewInfo);
        return userNewInfo;
    }
}
